package com.jgsu.sell.controller;

import com.jgsu.sell.converter.OrderForm2OrderDtoConverter;
import com.jgsu.sell.dto.OrderDto;
import com.jgsu.sell.enums.ResultEnum;
import com.jgsu.sell.exception.SellException;
import com.jgsu.sell.from.OrderForm;
import com.jgsu.sell.service.BuyerService;
import com.jgsu.sell.service.OrderService;
import com.jgsu.sell.utils.ResultVoUtil;
import com.jgsu.sell.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述:
 * 订单
 *
 * @author lqd12
 * @create 2018-08-19 23:12
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private BuyerService buyerService;

    /**
     * 创建订单
     * @return
     */
    @PostMapping("create")
    public ResultVO<Map<String,String>> create(@Valid OrderForm orderForm, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("【创建订单】，参数不正确，orderForm={}",orderForm);
            throw new SellException(ResultEnum.PARAM_ERROR.getCode()
                    ,bindingResult.getFieldError().getDefaultMessage());
        }
        OrderDto orderDto = OrderForm2OrderDtoConverter.converter(orderForm);
        if(CollectionUtils.isEmpty(orderDto.getOrderDetailList())){
            log.error("【创建订单购物车不能为空】");
            throw new SellException(ResultEnum.CART_EMPTY);
        }
        OrderDto result = orderService.create(orderDto);
        Map<String ,String> map = new HashMap<>();
        map.put("orderId",result.getOrderId());
        return ResultVoUtil.success(map);
    }
    /**
     * 订单列表
     */
    @GetMapping("list")
    public ResultVO<List<OrderDto>> list(@RequestParam("openid")String openid,
                                         @RequestParam(value = "page" ,defaultValue = "0") Integer page,
                                         @RequestParam(value = "size" ,defaultValue = "10") Integer size){
        if(StringUtils.isEmpty(openid)){
            log.error("【查询订单列表】openid为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        PageRequest pageRequest = new PageRequest(page,size);
        Page<OrderDto> orderDtoPage = orderService.findList(openid, pageRequest);
        return ResultVoUtil.success(orderDtoPage.getContent());
    }

    /**
     * 订单详情
     * @param openid
     * @param orderId
     * @return
     */
    @GetMapping("detail")
    public ResultVO<OrderDto> detail(@RequestParam("openid")String openid,
                                     @RequestParam("orderId")String orderId){
        OrderDto orderDto = buyerService.findOrderOne(openid, orderId);
        return ResultVoUtil.success(orderDto);
    }

    /**
     * 订单取消
     * @param openid
     * @param orderId
     * @return
     */
    @PostMapping("cancel")
    public ResultVO<OrderDto> cancel(@RequestParam("openid")String openid,
                                     @RequestParam("orderId")String orderId){
        buyerService.cancelOrder(openid,orderId);
        return ResultVoUtil.success();
    }
}